package org.gongliang.jsmile.authc.sys.controller;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.gongliang.jsmile.authc.sys.entity.SysResources;
import org.gongliang.jsmile.authc.sys.entity.SysRole;
import org.gongliang.jsmile.authc.sys.service.SysResourcesService;
import org.gongliang.jsmile.authc.sys.service.SysRoleService;
import org.gongliang.jsmile.common.base.BaseController;
import org.gongliang.jsmile.common.base.JSmileResult;
import org.gongliang.jsmile.common.constants.KConstant;
import org.gongliang.jsmile.common.enums.KException;
import org.gongliang.jsmile.common.kit.web.ShiroKit;
import org.gongliang.jsmile.common.kit.web.WebKit;
import org.gongliang.jsmile.config.log.info.KLog;
import org.gongliang.jsmile.config.log.info.LogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @desc 控制器
 * @author 龚亮
 * @version 1.0
 * @date 2017年05月15日 02:52:17
 */
@Controller
@RequestMapping("/sys_role")
@Api(tags = "角色管理")
public class SysRoleController extends BaseController {

	@Autowired
	private SysRoleService roleService;
	@Autowired
	private SysResourcesService sysResourcesService;

	@GetMapping("/main")
	public String main() {
		return "sys/Role/role";
	}
	@GetMapping("/per")
	public String per(Model model, String roleId) {
		model.addAttribute("roleId", roleId);
		return "sys/Role/per";
	}
	@GetMapping("/add")
	public String add() {
		return "sys/Role/add";
	}
	@GetMapping("/edit")
	public String edit() {
		return "sys/Role/edit";
	}

	/**
	 * 查询所有分页 返回 Lit<Map>
	 */
	@ResponseBody
	@RequestMapping("/findAllByPage")
	@ApiOperation(value = "获取角色列表", notes = "分页获取角色列表")
	@KLog(title = "角色管理->分页获取角色列表", type = LogType.QUERY)
	public JSmileResult findAllByPage(HttpServletRequest request) {
		Map<String, Object> reqMap = WebKit.getParams(request);
		WebKit.setPage(WebKit.toPageIndex(reqMap), WebKit.toPageSize(reqMap));
		List<SysRole> list = roleService.findAll(reqMap);
		PageInfo<SysRole> pages = new PageInfo<SysRole>(list);
		JSmileResult result = JSmileResult.success();
		result.setResData(pages.getList(), pages.getTotal());
		return result;
	}

	@ResponseBody
	@RequestMapping("/findAll")
	@ApiOperation(value = "获取角色列表", notes = "获取所有角色列表(不分页)")
	@KLog(title = "角色管理->获取角色列表(不分页)", type = LogType.QUERY)
	public JSmileResult findAll(HttpServletRequest request) {
		Map<String, Object> reqMap = WebKit.getParams(request);
		List<SysRole> list = roleService.findAll(reqMap);
		JSmileResult result = JSmileResult.success();
		result.setResData(list, list.size() + 0L);
		return result;
	}

	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:role:add")
	@KLog(title = "角色管理->添加角色信息", type = LogType.INSERT)
	public JSmileResult save(SysRole role,HttpServletRequest request) {
		JSmileResult result=JSmileResult.success("保存角色信息成功!");
		role.setCreateDate(new Date());
		role.setCreator(ShiroKit.getUserId());
		if(!roleService.saveOne(role)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("保存角色信息失败!");
		}
		return  result;
	}
	@ResponseBody
	@PostMapping("/update")
	@RequiresPermissions("sys:role:update")
	@KLog(title = "角色管理->修改角色信息", type = LogType.UPDATE)
	public JSmileResult update(SysRole role,HttpServletRequest request) {
		JSmileResult result=JSmileResult.success("修改角色信息成功!");
		role.setUpdater(ShiroKit.getUserId());
		role.setUpdateDate(new Date());
		if(!roleService.updateById(role)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("修改角色信息失败!");
		}
		return  result;
	}


	/**
	 * 根据一组id删除
	 */
	@ResponseBody
	@PostMapping("/deleteByIds")
	@RequiresPermissions("sys:role:delete")
	@KLog(title = "角色管理->删除角色信息", type = LogType.DELETE)
	public JSmileResult deleteByIds(@RequestParam(value = "ids[]", required = true) String[] ids) {
		JSmileResult result=JSmileResult.success();
		if (ids == null || ids.length == 0) {
			return JSmileResult.error(KException.IDS_NOT_NULL);
		}
		if(!roleService.deleteRoles(ids)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("批量删除用户失败!");
		}
		return  result;
	}


	/*
	 * desc:根据角色id获取角色资源信息
	 */
	@ResponseBody
	@RequestMapping("/getRoleResources")
	public JSmileResult getRoleResource(@RequestParam(value = "roleId", required = true) Integer roleId,HttpServletRequest request) {
		JSmileResult result = JSmileResult.success("请求成功!");
		SysRole role=new SysRole();
		role.setId(roleId);
		List<SysRole> roles=Lists.newArrayList();
		roles.add(role);
		Map<String,Object> param=WebKit.getParams(request);
		param.put("menuType", KConstant.ALL);
		param.put("roleIds", roles);
		List<SysResources> list = sysResourcesService.getMenuByRoles(param);
		result.setResData(list);
		return result;
	}
	/*
	 * desc:保存角色资源
	 */
	@ResponseBody
	@PostMapping("/saveRoleResources")
	@KLog(title = "角色管理->保存角色权限", type = LogType.INSERT)
	public JSmileResult saveRoleResource(@RequestParam(value = "roleId", required = true) Integer roleId,
			@RequestParam(value = "resourcesIds[]", required = false) List<Integer> resourceIds) {
		if (roleService.saveRoleResources(roleId, resourceIds)) {
			return JSmileResult.success("保存角色资源成功!");
		}
		return JSmileResult.error("保存角色资源失败!");
	}
}
